import orm_market
#   业务层：实现功能逻辑
#1.查看所有商品信息；
def show_products():
    sql = "select*from products"
    data = orm_market.read_data(sql)
    print('            ----商品列表----')
    if data:
        for products in data:
            print(f"编号：{products[1]},名称：{products[2]},单价：{products[3]},折扣：{products[4]}")
    else:
        print("没有商品信息")
def get_product():
    num = input('请输入要查询的商品编码：')
    sql = f"""
        select * from products where num='{num}'
    """
    data = orm_market.read_data(sql)
    print('            ----商品列表----')
    if data:
        for products in data:
            print(f"编号：{products[1]},名称：{products[2]},单价：{products[3]},折扣：{products[4]}")
    else:
        print("没有商品信息")

def add_product():
    num = input("请输入商品编号：")
    name = input("请输入商品名称：")
    price = input("请输入商品单价：")
    discount = input("请输入商品折扣：")
    sql = f"insert into products (num, name, price, discount) values " \
          f"('{num}', '{name}', '{price}', '{discount}')"
    rows = orm_market.writer_data(sql)
    if rows > 0:
        print('商品添加成功')
    else:
        print('商品添加失败')

def mod_product():
    num = input('请输入要修改的的商品编码：')
    price = input("请输入要修改商品单价：")
    discount = input("请输入商品折扣（0-1之间）：")
    sql = f"""
            update products set price={price},discount={discount} where num='{num}'
        """
    rows = orm_market.writer_data(sql)
    if rows > 0:
        print('商品单价修改成功')
    else:
        print('商品单价修改失败')

def del_product():
    num = input("请输入商品编号：")
    sql = f"delete from products where num='{num}'"
    rows = orm_market.writer_data(sql)
    if rows > 0:
        print('商品删除成功')
    else:
        print('商品删除失败')
def show_orders():
    sql = "select*from orders"
    data = orm_market.read_data(sql)
    print('          -----订单表-----')
    if data :
        for orders in data:
            print(f"编号：{orders[1]}，货物数量：{orders[2]}，金额：{orders[3]}")
    else:
        print('没有订单信息')
def del_order():
    num = input("请输入商品编号：")
    sql = f"delete from orders where num='{num}'"
    rows = orm_market.writer_data(sql)
    if rows > 0:
        print('订单删除成功')
    else:
        print('订单删除失败')
def count_orders():
    sql = "select sum(count), sum(amount) from orders"
    data = orm_market.read_data(sql)
    if data:
        print(f"总销量：{data[0][0]}，总销售额：{data[0][1]}")
    else:
        print("获取订单统计信息失败")

def add_order():
    amount = 0
    count = 0
    while True:
        data = get_product()
        if data != ():
            num = int(input('请输入要购买的数量：'))
            money = data[0][2] * num
            count += num
            amount += money
            choice = int(input('请输入你的选择1继续2退出：'))
            if choice == 1:
                continue
            else:
                break
    print(f'总数量{count}总金额{amount}')
    sql = f"""
            select max(num) from orders
        """
    data = orm_market.read_data(sql)
    id = data[0][0] + 1
    sql = f"""
            insert into orders values(null,{id},{count},{amount})
        """
    rows = orm_market.writer_data(sql)
    if rows > 0:
        print('订单写入成功')
    else:
        print('订单写入失败')
if __name__ == '__main__':
# 测试一下 show_products
    #show_products()
    #get_product()
    #add_product()
    #mod_product()
    #del_product()
    #show_orders()
    #del_order()
    #count_orders()
    add_order()